# Copyright (c) The PyAMF Project.
# See LICENSE.txt for details.

import glob
import os.path
import tempfile
import os

from django.http import HttpResponse, get_host
from pyamf.flex import ArrayCollection

import gateway
from amf.settings import sound_root
from sound.models import *

max_result = 50
file_types = 'jpg,png'
base_url = 'http://%s/images/'


def save_sound(request, sound_data, type,model,pk,lang,term):
    print model
    print pk
    print lang
    print sound_root
   
    sound = Sound.save_sound(lang,model,pk,term)
    try:
        #print sound_root+lang
        os.mkdir(sound_root+lang)
    except:
        pass
    fname = "%s%s/%d.%s"%(sound_root,lang,sound.pk,type)

    #print fname
    fp = open(fname, 'wb+')
    fp.write(sound_data.getvalue())
    fp.close()
    sound.userfile=fname
    sound.save()

    url = base_url % get_host(request)
    name = fp.name[len(sound_root) + 1:]

    return {
        'url': url + name,
        'name': name
    }
